/*
 * @Author: 9ilk 3535851673@qq.com
 * @Date: 2025-01-15 20:36:53
 * @LastEditors: 9ilk 3535851673@qq.com
 * @LastEditTime: 2025-01-16 13:17:30
 * @FilePath: /zhuang/TCP/EchoServer/TcpClient.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<cstring>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<iostream>
#include<unistd.h>
using namespace std;

//./client_tcp
int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        cout << "Usage:./client_tcp  server_ip  server_port\n";
        return 1;
    }
    string server_ip = argv[1];
    int server_port = atoi(argv[2]);
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
        cout << "create socket failed\n";
        return 2;
    }
    //填充套接字地址结构体
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);//主机转网络序列
    server_addr.sin_addr.s_addr = inet_addr(server_ip.c_str());//点分式字符串风格ip地址转换为网络字节序
    //client 不需要显示进行bind --- 
    //需要操作系统进行自动绑定 客户端连别人,别人不可能连它 --- connect的时候系统自动分配端口
    //首次连接自动bind
    int n = ::connect(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));
    if(n < 0)
    {
        cout << "connect failed\n";
        return 3;
    }
    //连接成功
    string message;
    while(true)
    {  //发消息
        char inbuffer[1024];
        cout << "input message: ";
        getline(cin,message);
        n = write(sockfd,message.c_str(),message.size());
        if(n > 0) //读消息
        {
            int m = ::read(sockfd,inbuffer,sizeof(inbuffer));
            if(m > 0)
            {  //读取成功
                inbuffer[m] = 0;
                cout << inbuffer << endl;
            }
            else
             break;
        }
        else
         break;
    }
   ::close(sockfd); 
  return 0;
}